package com.zthz.douxing.utils

import com.zthz.douxing.command.RestTicketCommand
import com.zthz.douxing.exception.CurrentIpNotFoundException

import com.zthz.douxing.system.PlatformType
import grails.util.Environment
import groovy.time.BaseDuration

/**
 * Created by zsw on 5/1/14.
 */
class  CommonUtil {

    def  static String CRON_EXPRESSION = "0 0 0/2 * * ?"
    def static int EXPIRE_Tiem = 2* 60 * 60

    private static final String DEFAULT_TIME = "yyyy-MM-dd hh:mm:ss"
    private static String WHEN_START = null



    public static Date getDate(String date){
        return getDate(DEFAULT_TIME,date)
    }


    public static String getDateStr(String timePattern,String date){
        return new Date().parse(timePattern,date).format(timePattern)
    }

    public static Date getDate(String timePattern,String date){
        def pattern = timePattern ? timePattern : DEFAULT_TIME
        return  new Date().parse(pattern,date)
    }


    private setStringDate(long time){
        StringBuffer sb = new StringBuffer()
        def _time = time / 1000*60*60
        WHEN_START =  sb.append(""+_time).toString()
    }

    public static String getVerplusStringDate(){
        return  WHEN_START
    }

    /**
     * 获取当前服务器的其中一个ip地址
     * @return
     */
    public static  String getInet4Address(){
        def currentIp = ""
        NetworkInterface.getNetworkInterfaces().each{ it ->
            if(currentIp){
                return
            }
            currentIp = it.addrs.find {
                (it.toString() =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.{1,3}/) && ( it.toString() != "/127.0.0.1")
            }?.toString() - "/"
        }

        return currentIp
    }

    public static boolean isMasterHost(){
        def masterFile = new File("/server/master")
        return masterFile.exists()
    }

    public static String getJSONFromateDataFromMap(HashMap map){

        String result ="{";
        Set set = map.keySet()

        for(String key : set){
            result+=key+":'"+map.get(key)+"',"
        }

        return result.substring(0,result.size()-1)+"}"

    }

    public static String getPlantform2MessageApp(PlatformType platformType){
        switch (platformType){

            case PlatformType.android : return "1"
                break
            case PlatformType.ios : return "0"
        }
    }
    public static String getJobKey(String type,String value,Date sendTime){

        if(value){
            return "${Environment.current.getName()}-${type}-${value}-${new Date().getTime()}-${sendTime.getTime()}"
        } else {
            return "${Environment.current.getName()}-${type}-${new Date().getTime()}-${sendTime.getTime()}"
        }
    }

    public static String getRestTicketKey(RestTicketCommand command){

        return command.fromCity+":"+command.toCity+":"+command?.trainNo+":"+getDateStr("yyyy-MM-dd",command.departDate)
    }

    public static String getTaskInfoKeyRegExp(String type){
        return "${Environment.current.getName()}-${type}*"
    }

    public static Date getFromDuration2Date(BaseDuration date){

       return new Date(date.toMilliseconds())
    }

}
